package com.example.anyong17_julnumgi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.SumPathEffect;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class RecordActivity extends Activity {
        ArrayList<Record>arRecord = new ArrayList<Record>();
        int date_count=0;
        
        TextView mTime;
        TextView mCount;
        
        
        
        Today today=new Today();
        String mainToday=today.dTime;
        
        String filename = mainToday;
        
        
        String str = "";
        String path = "/data/data/com.example.anyong17_julnumgi/files"+"/"+filename;
        String dirPath="/data/data/com.example.anyong17_julnumgi/files/";
        
        
        File dir = new File(dirPath);
        File[] files = dir.listFiles();
        int recodePage = 0;//4
        
        String strColor = "#F6EB14";
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.recordactivity);
                Intent intent = getIntent();
                arRecord = intent.getParcelableArrayListExtra("Recoders");
                
                mTime = (TextView) findViewById(R.id.record_tv1);
                mCount= (TextView) findViewById(R.id.record_tv2);
                
                if(dir.exists()==true){
                	recodePage= files.length-1;
                }else{
                	recodePage=0;
                }
                        
                File file = new File(path);
                
                if(arRecord.isEmpty()==true){//레코드 없음, 메뉴화면에서 넘어옴
                        mCount.setText("기록이 없음");
                        if(file.exists()==false){
                                mTime.setText(filename);
                                mCount.setText("기록이 없습니다.");
                        }else{
                                loadfile();//파일을 str저장
                                mTime.setText(filename);
                                mCount.setText(str);//파일 -> str -> 출력
                        }
                }else{//JUL액티비티에서 넘어왔을떄
                        if(file.exists()==false){//파일 없을 시
                                //mTime.setText(mainToday+"\n");
                                mTime.setText(filename);
                                printRecord();//레코드를 str저장  
                                mCount.setText(str);
                                savefile();//str 한꺼번에 저장, 추가저장없음
                                
                                //Toast.makeText(RecordActivity.this, "print", Toast.LENGTH_LONG).show();
                                 
                        }else{// 파일 있을시
                                //mTime.setText(mainToday+"\n");
                                mTime.setText(filename);
                                loadfile();//파일을 str저장
                                printRecord();//레코드를 str저장
                                mCount.setText(str);
                                savefile();//str 한꺼번에 저장, 추가저장없음
                                arRecord.clear();
                                //Toast.makeText(RecordActivity.this, "load, print", Toast.LENGTH_LONG).show();
                        }
                }
                        
                        
                        
        }
        
        
      
        public void mOnClick (View v){
                switch (v.getId()){
                        
                        case R.id.record_btn4://이전
                        	 if(dir.exists()==true){
                        		 recodePage--;
                                 if(recodePage<0)recodePage=0;
                                 testFileMake();
                                 mCount.setTextColor(Color.parseColor(strColor));
                                 mCount.setText(str);
                                 if(files==null){
                                	 Toast.makeText(RecordActivity.this, "파일없음", Toast.LENGTH_SHORT).show();
                                 }else{
                                	 mTime.setText(files[recodePage].getName());
                                 }
                                 Toast.makeText(RecordActivity.this, "이전 날짜", Toast.LENGTH_SHORT).show();
                                 break;
                        	 }else{
                        		 Toast.makeText(RecordActivity.this, "파일없음 이동금지", Toast.LENGTH_SHORT).show();
                        		 break;
                        	 }
                        		
                        case R.id.record_btn5://이후
                        	if(dir.exists()==true){ 
                        		recodePage++;
                        		if(files==null){
                                  	 recodePage=0;
                                   }else{
                                	   if(recodePage>files.length-1)recodePage=files.length-1;
                                	   }
                                testFileMake();
                                mCount.setTextColor(Color.parseColor(strColor));
                                mCount.setText(str);
                                if(files==null){
                               	 Toast.makeText(RecordActivity.this, "파일없음", Toast.LENGTH_SHORT).show();
                                }else{
                               	 mTime.setText(files[recodePage].getName());
                                }
                                Toast.makeText(RecordActivity.this, "이후 날짜", Toast.LENGTH_SHORT).show();
                                break;
                        	}else{
                       		 Toast.makeText(RecordActivity.this, "파일없음 이동금지", Toast.LENGTH_SHORT).show();
                       		 break;
                       	 }  
                        case R.id.record_btn6://목록
                        	if(dir.exists()==true){
                        		mCount.setTextColor(Color.RED);
                        		if(files==null){
                        			mCount.setText("기록이 없습니다");
                        		}else{
                        		mCount.setText(listFile());
                        			}
                        		}else{Toast.makeText(RecordActivity.this, "파일없음", Toast.LENGTH_SHORT).show();}
                                break;
                }
        }
        public void printRecord() {
                for(int i = 0;i<arRecord.size();i++){
                        String count = arRecord.get(i).counter; 
                        String time = arRecord.get(i).time;
                        str+=String.format("%s 바퀴 %s 초\n", count ,time);
                        }
        }
        
        public void loadfile() {
                try{
                        FileInputStream fis = openFileInput(filename);
                        byte[] data = new byte[fis.available()];
                        while (fis.read(data)!=-1) {;}
                        fis.close();
                        str=(new String(data));
                        }catch (Exception e) {;}
        }
        public void savefile() {
                try{
                        FileOutputStream fos = openFileOutput(filename, MODE_PRIVATE);
                        fos.write(str.getBytes());
                        fos.close();
                        }catch(Exception e){;}
        }
        public void delfile() {
                deleteFile(filename);//오늘 일자만 지움
                //deleteFile("2013년 09월 09일0.txt");//강제 삭제
        }
        private String listFile() {
                String islistFile= "";
                File dir = new File(dirPath);
                File[] tempfiles = dir.listFiles();
                
	                if(tempfiles==null){
	                        Toast.makeText(RecordActivity.this, "리스트 없음", Toast.LENGTH_LONG).show();
	                }else if(dir.exists()==true){
	                        for(int i=0;i<tempfiles.length;i++){
	                                        islistFile+=tempfiles[i].getName()+"\n";
	                                        files[i] =tempfiles[i];
	                                }
	                                return islistFile;
	                        
                }else{
                	Toast.makeText(RecordActivity.this, "파일없음", Toast.LENGTH_LONG).show();
                }
                return "일차없음";
        }
        public void testFileMake() {
                /*try{
                        FileOutputStream fos = openFileOutput("2.txt", MODE_PRIVATE);
                        fos.write(str.getBytes());
                        fos.close();
                        }catch(Exception e){;}*/
                try{
                        FileInputStream fis = openFileInput(files[recodePage].getName());
                        byte[] data = new byte[fis.available()];
                        while (fis.read(data)!=-1) {;}
                        fis.close();
                        str=(new String(data));
                        }catch (Exception e) {
                        	Toast.makeText(RecordActivity.this, "리스트 없음", Toast.LENGTH_LONG).show();}
        	}
        @Override
        protected void onDestroy() {
        	 RecycleUtils.recursiveRecycle(getWindow().getDecorView());
             System.gc();
             super.onDestroy();
        }
}